#pragma once

#include <iostream>
#include <functional>
#include <mutex>
#include "httplib.h"
#include "conf.hh"
#include "util.hh"
#include "data.hh"

namespace cloud
{
class client
{
public:
    using business = std::function<void(const httplib::Request&, httplib::Response&)>;

    static client& instance()
    {
        static client cli(config::instance().server_ip(),
                config::instance().server_port());
        return cli;
    }

    bool upload_by_bpath(const std::string& bpath)
    {
        backup_info* info = _data_mgr.get_by_bpath(bpath);

        if (!info)
        {
            easylog::warn("get info error");
            return false;
        }

        httplib::MultipartFormData item;

        item.name = "file";
        item.filename = info->fpath;
        item.content = info->content();
        item.content_type = "application/octet-stream";

        auto res = _cli.Post("/upload", {item});

        return res && res->status == 200;
    }

    bool delete_by_bpath(const std::string& bpath)
    {
        return false;
    }

private:
    client(const std::string& sip, uint16_t sport)
        : _cli(sip, sport)
        , _data_mgr(data_manager::instance())
    {
        easylog::info("http client created succ");
    }

private:
    httplib::Client _cli;
    data_manager& _data_mgr;
};

}
